home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / CAMP.C < prev    next >
C/C++ Source or Header  |  1992-03-09  |  17KB  |  510 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : CAMP.C
  4.  *
  5.  *      This is where the original CAMP algorithmn is implemented.
  6.  *
  7.  * --------------------------------------------------------------------------
  8.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  9.  *    University.
  10.  *
  11.  *    You are free to use, copy and distribute this software and its
  12.  *    documentation providing that:
  13.  *
  14.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  15.  *
  16.  *        IT IS NOT MODIFIED IN ANY WAY.
  17.  *
  18.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  19.  *
  20.  *    This program is provided "AS IS" without any warranty, expressed or
  21.  *    implied, including but not limited to fitness for any particular
  22.  *    purpose.
  23.  *
  24.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  25.  *    appreciated. Please send to:
  26.  *
  27.  *        Boon-Tiong Tan or Othman Bin Ahmad
  28.  *        School of EEE
  29.  *        Nanyang Technological University
  30.  *        Nanyang Avenue
  31.  *        Singapore 2263
  32.  *        Republic of Singapore
  33.  *
  34.  ***************************************************************************/
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #define mask8 255
  40.  
  41. unsigned char   *camp(a, b)              /* pointer to a & b array passed */
  42. unsigned char   *a, *b;
  43.  
  44. {
  45.    unsigned short   pterm, ma, mb, pos;
  46.    unsigned short   *ca, *mp, m_cnt;
  47.    unsigned long    m, j, size, addr, count, count1, topow();
  48.    register long    i, wo, wi;
  49.          char   test;
  50.    unsigned  char   *c, *d, *e, *s, *temp, *cp, *mt, *at, *ad;
  51.    unsigned  char   n, adj, adj0, adji, adjacency(), nspm, cover;
  52.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  53.  
  54.  
  55.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  56.  
  57.    n    = *a;                          /* no. of variables */
  58.    nspm = *(a+3);                      /* no. of bytes/minterm */
  59.    ma = *(a+2)<<8 | *(a+1);           /* no. of minterms in a-array */
  60.  
  61.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  62.    if (temp == 0)
  63.       {
  64.      printf("Out of memory -- CAMP, *temp\n");
  65.      printf("Program terminated - 1\n");
  66.      exit(0);
  67.       }
  68.  
  69.    s = (unsigned char *) malloc(4);                /* header of s-array */
  70.    if (s == 0)
  71.       {
  72.      printf("Out of memory -- CAMP, *s\n");
  73.      printf("Program terminated - 2\n");
  74.      exit(0);
  75.       }
  76.  
  77.  
  78.    /***********\
  79.     * PHASE I *
  80.    \***********/
  81.  
  82.    pterm = 0;                                /* no. of product term */
  83.    count = 0;                                /* no. of terms stored for phase II */
  84.  
  85.    /***
  86.     ***  DETERMINE ADJACENCY OF ALL MINTERMS AND GENERATE THEIR CPT
  87.     ***/
  88.  
  89.    for (i=0; i<mb; i++)                      /* for all minterms in b-array */
  90.       {
  91.      *b = n;                             /* assign back to n */
  92.  
  93.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  94.      c = adjacent(temp, a, 255);         /* obtain the adjacent terms */
  95.      adj = *(c+1);                       /* adjacency of minterm */
  96.      d = cpt(c);                         /* generate CPT */
  97.  
  98.      /***
  99.       ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  100.       ***/
  101.  
  102.      if (adj <= 1)                       /* adjacency 0 or 1 */
  103.         {
  104.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  105.            if (s == 0)
  106.           {
  107.              printf("Out of memory -- CAMP, *s\n");
  108.              printf("Program terminated - 3\n");
  109.              exit(0);
  110.           }
  111.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  112.            pterm++;                      /* product term count */
  113.  
  114.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  115.            i = (char) *b;                /* index pointer of b-array */
  116.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  117.         }
  118.      else                                /* adj > 1 */
  119.         {
  120.  
  121.            /***
  122.         ***  MINTERMS WITH ADJACENCY GREATER THAN 1, GENERATE SSM AND TEST FOR COVERAGE
  123.         ***/
  124.  
  125.            e = ssm(d);                          /* generate SSM */
  126.            m = topow(*(e+1));                   /* no. of SSM terms */
  127.  
  128.            for (j=0; j<m; j++)                  /* check for SSM coverage */
  129.           {
  130.              memcpy (temp, (e+4+nspm*j), nspm);   /* pick SSM term */
  131.              test = exist (temp, a, ma);
  132.              if (test == -1)                /* minterm not in a-array */
  133.             break;
  134.           }
  135.  
  136.            /***
  137.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  138.         ***/
  139.  
  140.            if (test == 0)                        /* all SSM's covered by fn */
  141.           {
  142.              if (m > 65536)                  /* too many SSM terms */
  143.             {
  144.                printf("Product Term Too Huge \n");
  145.                printf("Program terminated \n");
  146.                exit(0);
  147.             }
  148.              *e = n;                         /* no. of variables */
  149.              *(e+1) = (m-1) & mask8;         /* no. of SSM terms minus 1 */
  150.              *(e+2) = (m-1)>>8 & mask8;
  151.              b = reduce(e,b,i);              /* remove minterms covered from b-array */
  152.              i = (char) *b;                  /* index pointer of b-array */
  153.              mb = *(b+2)<<8 | *(b+1);        /* no. of minterms in b-array */
  154.  
  155.              s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  156.              if (s == 0)
  157.             {
  158.                printf("Out of memory -- CAMP, *s\n");
  159.                printf("Program terminated - 4\n");
  160.                exit(0);
  161.             }
  162.              memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  163.              pterm++;                       /* no. of product terms */
  164.           }
  165.            else
  166.           {
  167.              /***
  168.               ***  NOT COVERED, STORE UNCOVERED MINTERMS, ITS CORRESPONDING
  169.               ***  ADJACENCY, ADJACENT TERMS AND CPT FOR PHASE II
  170.               ***/
  171.  
  172.              count++;                       /* no. of terms for phase II */
  173.              if (count==1)                  /* 1st uncovered term */
  174.             {
  175.                ad = (unsigned char *) malloc(1);    /* array of adjacency */
  176.                if (ad==0)
  177.                   {
  178.                  printf("Out of memory -- CAMP, *ad\n");
  179.                  printf("Program terminated - 5\n");
  180.                  exit(0);
  181.                   }
  182.                cp = (unsigned char *) malloc(n);     /* array of CPT's */
  183.                if (cp==0)
  184.                   {
  185.                  printf("Out of memory -- CAMP, *cp\n");
  186.                  printf("Program terminated - 6\n");
  187.                  exit(0);
  188.                   }
  189.                mt = (unsigned char *) malloc(4+nspm*count);  /* array of minterms */
  190.                if (mt==0)
  191.                   {
  192.                  printf("Out of memory -- CAMP, *mt\n");
  193.                  printf("Program terminated - 7\n");
  194.                  exit(0);
  195.                   }
  196.                size = 4 + nspm*(1+adj);                         /* addr counter of at-array */
  197.                at = (unsigned char *) malloc (4+nspm*(1+adj));  /* all adj terms from c-arrays */
  198.                if (at==0)
  199.                   {
  200.                  printf("Out of memory -- CAMP, *at\n");
  201.                  printf("Program terminated - 8\n");
  202.                  exit(0);
  203.                   }
  204.                ca = (unsigned short *) malloc(2);          /* array of addr of at-array */
  205.                if (ca==0)
  206.                   {
  207.                  printf("Out of memory -- CAMP, *ca\n");
  208.                  printf("Program terminated - 9\n");
  209.                  exit(0);
  210.                   }
  211.                addr = 0;         /* starting address of at-array */
  212.             }
  213.              else                    /* subsequent uncovered terms */
  214.             {
  215.                ad = (unsigned char *) realloc(ad, count);    /* adjacency */
  216.                if (ad==0)
  217.                   {
  218.                  printf("Out of memory -- CAMP, *ad\n");
  219.                  printf("Program terminated - 10\n");
  220.                  exit(0);
  221.                   }
  222.                cp = (unsigned char *) realloc(cp,n*count);    /* CPT's */
  223.                if (cp==0)
  224.                   {
  225.                  printf("Out of memory -- CAMP, *cp\n");
  226.                  printf("Program terminated - 11\n");
  227.                  printf("n = %d, count = %d\n", n, count);
  228.                  exit(0);
  229.                   }
  230.                mt = (unsigned char *) realloc(mt,4+nspm*count); /* minterms */
  231.                if (mt==0)
  232.                   {
  233.                  printf("Out of memory -- CAMP, *mt\n");
  234.                  printf("Program terminated - 12\n");
  235.                  exit(0);
  236.                   }
  237.                size+=4+nspm*(1+adj);                     /* addr counter of at-array */
  238.                at = (unsigned char *) realloc (at,size); /* all adj terms from c-arrays */
  239.                if (mt==0)
  240.                   {
  241.                  printf("Out of memory -- CAMP, *mt\n");
  242.                  printf("Program terminated - 13\n");
  243.                  exit(0);
  244.                   }
  245.                ca = (unsigned short *) realloc(ca,2*count);       /* addr of at-array */
  246.                if (ca==0)
  247.                   {
  248.                  printf("Out of memory -- CAMP, *ca\n");
  249.                  printf("Program terminated - 14\n");
  250.                  exit(0);
  251.                   }
  252.             }
  253.  
  254.              /***
  255.               ***  STORE IN ARRAYS FOR PHASE II
  256.               ***/
  257.  
  258.              *(ad+(count-1)) = adj;                           /* adjacency */
  259.              memcpy((cp+n*(count-1)),d,n);                    /* CPT's */
  260.              memcpy((mt+4+nspm*(count-1)),(b+4+nspm*i),nspm); /* minterms */
  261.              memcpy((at+addr),c,4+nspm*(1+adj));              /* adj terms */
  262.              *(ca+(count-1)) = addr;                   /* addr of at-array */
  263.              addr = size;                              /* update addr */
  264.           }
  265.            free(e);                       /* free pointer */
  266.         }
  267.      free(c);                     /* free pointers */
  268.      free(d);
  269.       }
  270.    count1 = count;                   /* no. of terms for phase II */
  271.  
  272.  
  273.    /************\
  274.     * PHASE II *
  275.    \************/
  276.  
  277.    while (count > 0)               /* some terms stored for phase II */
  278.       {
  279.      /***
  280.       ***  SELECT NEXT MINTERM WITH THE LOWEST ADJACENCY AND AT LEAST ONE OF
  281.       ***  ITS ADJACENT TERMS PREVIOUSLY COVERED
  282.       ***/
  283.  
  284.      adj0 = 255;               /* set to max of 8-bit */
  285.  
  286.      for (i=0; i<count1; i++)               /* do for all adj terms in phase II */
  287.         {
  288.            if (*(ad+i)<adj0 && *(ad+i)!=0)  /* choose minterms with lowest adj */
  289.           {
  290.              if (adj0 != 255)           /* not the first lowest */
  291.             free(mp);
  292.              adj0 = *(ad+i);            /* new lowest value */
  293.              m_cnt = 1;                 /* reset count to 1 */
  294.  
  295.              mp = (unsigned short *) malloc(2);     /* space for pos of lowest adj */
  296.              if (mp==0)
  297.             {
  298.                printf("Out of memory -- CAMP, *mp\n");
  299.                printf("Program terminated - 15\n");
  300.                exit(0);
  301.             }
  302.              *mp = i;                               /* first element */
  303.           }
  304.            else if (*(ad+i) == adj0)                    /* adj as large */
  305.           {
  306.              m_cnt++;                               /* no. of element in mp-array */
  307.              mp = (unsigned short *) realloc(mp, 2*m_cnt); /* more space */
  308.              if (mp==0)
  309.             {
  310.                printf("Out of memory -- CAMP, *mp\n");
  311.                printf("Program terminated - 16\n");
  312.                exit(0);
  313.             }
  314.              *(mp+m_cnt-1) = i;            /* elements in mp-array */
  315.           }
  316.         }
  317.  
  318.      /***
  319.       ***  SELECT FROM MP-ARRAY, A MINTERM WITH AT LEAST ONE ADJACENT TERM PREVIOSLY
  320.       ***  COVERED
  321.       ***/
  322.  
  323.      for (i=0; i<m_cnt; i++)                 /* do for all in mp-array */
  324.         {
  325.            adj = *(ad+ *(mp+i));             /* adjacency from ad-array */
  326.  
  327.            c = (unsigned char *) malloc(4+nspm*(1+adj));  /* space for adj terms */
  328.            if (c == 0)
  329.           {
  330.              printf("Out of memory -- CAMP, *c\n");
  331.              printf("Program terminated - 17\n");
  332.              exit(0);
  333.           }
  334.            memcpy(c, (at+ *(ca+ *(mp+i))), 4+nspm*(1+adj));  /* adj terms from at-array */
  335.  
  336.            for (j=0; j<adj; j++)          /* check for at least 1 adj term covered */
  337.           {
  338.              memcpy(temp, (c+4+nspm*(1+j)),nspm);
  339.              test = exist(temp,b,mb);
  340.              if (test == -1)          /* adj term covered */
  341.             break;
  342.           }
  343.  
  344.            if (test == -1)                /* adj term covered */
  345.           break;
  346.            else
  347.           free(c);                    /* free pointer */
  348.         }
  349.  
  350.      if (test == -1)                      /* adj term covered */
  351.         pos = *(mp+i);                    /* position of minterm required */
  352.      else                                 /* none of adj terms covered */
  353.         {
  354.            adj = *(ad+ *mp);              /* choose adj of 1st minterm */
  355.            pos = *mp;                     /* position of 1st minterm */
  356.  
  357.            c = (unsigned char *) malloc(4+nspm*(1+adj));   /* space for adj terms */
  358.            if (c == 0)
  359.           {
  360.              printf("Out of memory -- CAMP, *c\n");
  361.              printf("Program terminated - 18\n");
  362.              exit(0);
  363.           }
  364.            memcpy(c, (at+ *(ca+pos)), 4+nspm*(1+adj));     /* adj terms from at-array */
  365.         }
  366.  
  367.      d = (unsigned char *) malloc(n+1);       /* space for corresponding CPT */
  368.      if (d == 0)
  369.         {
  370.            printf("Out of memory -- CAMP, *d\n");
  371.            printf("Program terminated - 19\n");
  372.            exit(0);
  373.         }
  374.      memcpy(d, (cp+n*pos), n);       /* corresponding CPT from cp-array */
  375.      *(d+n) = '\0';                  /* terminate with NULL string */
  376.  
  377.      e = ssm(d);                     /* generate SSM */
  378.  
  379.      free(d);                        /* free pointers */
  380.      free(mp);
  381.  
  382.      /***
  383.       ***  KEEP SHRINKING THE CPT UNTIL THE SELECTED MINTERM IS COVERED BY THE FUNCTION
  384.       ***/
  385.  
  386.      cover = 0;                      /* coverage status */
  387.      while (!cover)                  /* do until shrinked CPT is covered */
  388.         {
  389.            /***
  390.         ***  SELECT THE 1ST ADJACENT TERM WITH THE LOWEST WI TO SHRINK AWAY
  391.         ***  WI IS THE NO. OF TERMS ADJACENT TO THE ADJACENT TERMS THAT ARE
  392.         ***  SSM, E-ARRAY BUT NOT IN THE FUNCTION, A-ARRAY
  393.         ***/
  394.  
  395.            wo = -1;                  /* initial value */
  396.            for (i=0; i<adj; i++)     /* do for all adjacent terms */
  397.           {
  398.              memcpy(temp, (c+4+nspm*(i+1)), nspm);  /* pick adjacent term */
  399.  
  400.              wi = adj_of_mi(temp,e,a);              /* obtain wi */
  401.              if (wi>wo)
  402.             {
  403.                wo = wi;               /* new largest value */
  404.                pos = i;               /* position of adj terms */
  405.             }
  406.           }
  407.            free(e);                           /* free pointer */
  408.  
  409.            adj--;                             /* decrement adjacency */
  410.            *(c+1) = adj;                      /* update in c-array */
  411.  
  412.            memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos)); /* shrink c-array */
  413.  
  414.            c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* decrease space */
  415.            if (c == 0)
  416.           {
  417.              printf("Out of memory -- CAMP, *c\n");
  418.              printf("Program terminated - 20\n");
  419.              exit(0);
  420.           }
  421.  
  422.            d = cpt(c);                  /* generate CPT */
  423.            e = ssm(d);                  /* generate SSM */
  424.            m = topow(*(e+1));           /* no. of SSM terms */
  425.  
  426.            for (i=0; i<m; i++)          /* check SSM coverage by function */
  427.           {
  428.              memcpy(temp, (e+4+nspm*i), nspm);   /* pick SSM term */
  429.              test = exist(temp,a,ma);
  430.              if (test == -1)                     /* SSM not covered */
  431.             break;
  432.           }
  433.  
  434.            /***
  435.         ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY AND
  436.         ***  SELECT SHRINKED CPT AS PRODUCT TERM
  437.         ***/
  438.  
  439.            if (test==0)                      /* SSM covered */
  440.           {
  441.              if (m > 65536)                 /* too many SSM terms */
  442.             {
  443.                printf("Product Term Too Huge \n");
  444.                printf("Program terminated \n");
  445.                exit(0);
  446.             }
  447.              *e = n;                         /* no. of variables */
  448.              *(e+1) = (m-1) & mask8;         /* no. of SSM terms minus 1 */
  449.              *(e+2) = (m-1)>>8 & mask8;
  450.              b = reduce(e, b, m);        /* remove minterms covered from b-array */
  451.              mb = *(b+2)<<8 | *(b+1);    /* no. of minterms in b-array */
  452.  
  453.              count = mb;                 /* count of no. of minterms not covered */
  454.  
  455.              s = (unsigned char *) realloc(s, 4+n*(pterm+1));   /* more space */
  456.              if (s == 0)
  457.             {
  458.                printf("Out of memory -- CAMP, *s\n");
  459.                printf("Program terminated - 21\n");
  460.                exit(0);
  461.             }
  462.              memcpy ((s+4+n*pterm), d, n);     /* add shrinked CPT to soln array */
  463.              pterm++;                          /* no. of product terms */
  464.  
  465.              cover = 1;                        /* coverage status */
  466.  
  467.              /***
  468.               ***  FLAG TERMS COVERED IN THE AD-ARRAY
  469.               ***/
  470.  
  471.              for (i=0; i<m; i++)               /* flag terms just covered */
  472.             {
  473.                for (j=0; j<count1; j++)    /* do for all terms in mt-array */
  474.                   {
  475.                  test = memcmp((e+4+nspm*i), (mt+4+nspm*j), nspm);
  476.                  if (test==0)
  477.                     {
  478.                        *(ad+j) = 0;    /* flag the adjacency to indicate coverage */
  479.                        break;
  480.                     }
  481.                   }
  482.             }
  483.              free(e);            /* free pointer */
  484.           }
  485.            free (d);                 /* free pointer */
  486.         }
  487.      free(c);                        /* free pointer */
  488.       }
  489.  
  490.    if (count1>0)                  /* some terms stored for phase II */
  491.       {
  492.      free(ad);                /* free pointers */
  493.      free(cp);
  494.      free(mt);
  495.      free(at);
  496.      free(ca);
  497.       }
  498.  
  499.    *s = n;                        /* no. of variables */
  500.    *(s+1) = pterm & mask8;        /* no. of product terms in 2 bytes */
  501.    *(s+2) = pterm>>8 & mask8;
  502.  
  503.    free(temp);                    /* free pointer */
  504.    free(a);
  505.    free(b);
  506.  
  507.    return(s);                     /* return solution array */
  508. }
  509.  
  510.